package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.sife_secondactivity.OneSifeSecondActivity;
import com.wangtao.firstapp.modles.OneSifeSecondLook;

import java.util.List;

/**
 * 大家都在看 的适配器
 * Created by lanouhn on 16/9/5.
 */
public class OneSifeSecondLookAdapter extends RecyclerView.Adapter<OneSifeSecondLookAdapter.LookViewHolder> {

    private Context context;
    private OneSifeSecondLook oneSifeSecondLook;


    public OneSifeSecondLookAdapter(Context context, OneSifeSecondLook oneSifeSecondLook) {
        this.context = context;
        this.oneSifeSecondLook = oneSifeSecondLook;
    }

    @Override
    public LookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.actsifesecond_two_item_look, null);
        LookViewHolder holder = new LookViewHolder(view);
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(LookViewHolder holder, final int position) {
        List<OneSifeSecondLook.DataBean.TopicBean> topicBean = oneSifeSecondLook.getData().getTopic();
        if (topicBean.size() > 0) {
            holder.linearLayout_view_look_one.setVisibility(View.VISIBLE);
            if (position == 0) {
                holder.linearLayout_text.setVisibility(View.VISIBLE);
            } else {
                int i = position - 1;
                holder.linearLayout_content.setVisibility(View.VISIBLE);
                holder.nameOne.setText(topicBean.get(i).getTitle());

                if (topicBean.get(i).getPics().size()>0){
                    Picasso.with(context).load(topicBean.get(i).getPics().get(0).getUrl()).into(holder.photoOne);
                }

                if (!topicBean.get(i).getPic().equals("")){
                    Picasso.with(context).load(topicBean.get(i).getPic()).into(holder.photoOne);
                }
            }
        }

        //跳转至，点击的页面，即作者原创页面
        holder.linearLayout_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String item_ID=oneSifeSecondLook.getData().getTopic().get(position-1).getId();
                Intent intent=new Intent(context, OneSifeSecondActivity.class);
                intent.putExtra("item_ID",item_ID);
                context.startActivity(intent);

            }
        });


    }

    @Override
    public int getItemCount() {
        return oneSifeSecondLook.getData().getTopic().size()+1;
    }

    public class LookViewHolder extends RecyclerView.ViewHolder {
        private ImageView photoOne;
        private TextView nameOne;
        private LinearLayout linearLayout_view_look_one, linearLayout_text, linearLayout_content;

        public LookViewHolder(View itemView) {
            super(itemView);
            linearLayout_view_look_one = (LinearLayout) itemView.findViewById(R.id.linearLayout_view_look_one);
            photoOne = (ImageView) itemView.findViewById(R.id.photoOne_actsifesecond_two_item_look);
            nameOne = (TextView) itemView.findViewById(R.id.nameOne_actsifesecond_two_item_look);
            linearLayout_content = (LinearLayout) itemView.findViewById(R.id.linearLayout_content);
            linearLayout_text = (LinearLayout) itemView.findViewById(R.id.linearLayout_text);
        }
    }
}
